package com.kaba.security.handler;

import com.kaba.bean.SysMenu;
import com.kaba.xtzhgl.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.stereotype.Component;


import java.util.Collection;
import java.util.List;

/**
 * @Author lvmengjie
 * @Date 2021/3/24
 * @Description 安全元数据源
 */
@Component
public class CustomizeFilterInvocationSecurityMetadataSource implements FilterInvocationSecurityMetadataSource {
    @Autowired
    SysMenuService sysMenuService;
    @Override
    public Collection<ConfigAttribute> getAttributes(Object o) throws IllegalArgumentException {

            //获取请求地址
            String requestUrl = ((FilterInvocation) o).getRequestUrl();
            System.out.println("安全管理员中请求路径"+requestUrl);
            List<String> permissionCodeList = sysMenuService.queryBypath(requestUrl);
            if(permissionCodeList == null || permissionCodeList.size() == 0){
                //请求路径没有配置权限，表明该请求接口可以任意访问
                return null;
            }

            String[] attributes = new String[permissionCodeList.size()];
            for(int i = 0;i<permissionCodeList.size();i++){
                attributes[i] =permissionCodeList.get(i);
            }
            return SecurityConfig.createList(attributes);

        }

    @Override
    public Collection<ConfigAttribute> getAllConfigAttributes() {
        return null;
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return true;
    }

}
